﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DentistOffice
{
    public partial class Rejestracja : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DentistEntities dE = new DentistEntities();
                foreach (var dentist in dE.Employees.Where(qr => qr.positionId == 1))
                {
                    ListItem list = new ListItem();
                    list.Value = dentist.id.ToString();
                    list.Text = dentist.name + " " + dentist.surname;
                    DentistDropDownList.Items.Add(list);
                }
            }
            
        }              

        protected void Button2_Click(object sender, EventArgs e)
        {
            DentistEntities dE = new DentistEntities();         
            Patient pt = new Patient();

            var query = dE.Patients.Count(qr => qr.login == LoginTextBox.Text);

            
            if (int.Parse(query.ToString()) > 0)
            {
                infoLabel.Text = "Proszę zmienić login, gdyż poniższy już istnieje";
                LoginTextBox.Text = "";
                return;
            }
            else
            {
                pt.name = NameTextBox.Text;
                pt.surname = SurnameTextBox.Text;
                pt.phone = PhoneTextBox.Text;
                pt.login = LoginTextBox.Text;
                pt.email = EmailTextBox.Text;
                pt.password = Crypt.GetMD5Hash(PasswordTextBox.Text); //MD5
                infoLabel.Text = pt.password;


                if (DentistDropDownList.SelectedValue != "")
                {
                    pt.idDentist = int.Parse(DentistDropDownList.SelectedValue);                   
                }
                else
                {
                    infoLabel.Text = "Nie wybrano dentysty";
                    return;
                }
               /* if (NIPTextBox.Text != "")
                {
                    pt.nip = int.Parse(NIPTextBox.Text);
                }
                pt.companyName = CompanyNameTextBox.ToString();
                pt.city = CityTextBox.ToString();
                if(CityCodeTextBox.Text != "")
                {   
                    pt.cityCode = int.Parse(CityCodeTextBox.Text);
                }
                pt.street = StreetTextBox.ToString();*/
                try
               {
                    dE.AddToPatients(pt);
                    dE.SaveChanges();
                    Response.Redirect("~/RegisterThanks.aspx");
               }
               catch (Exception ex)
               {
                    infoLabel.Text = ex.Message.ToString();
               }
             
            }
        }

        

    }
}